<?php

include_once 'common.php';

$filter = "WHERE status > -1";

if ($payload) {
  foreach ($payload as $key => $value) {
    if (count($value) > 0) {
      $filter .= " AND {$key} IN ('" . implode("','", $value) . "')";
    }
  }
}

// $sql = "SELECT * FROM `trex_records` {$filter} ORDER BY `trainingDate` DESC, `title` DESC";
$sql = "SELECT * FROM `trex_records` {$filter} ORDER BY `number` DESC";
$result = exec_sql($sql);
$output = [];
if ($result) {
  foreach ($result as $key => $value) {
    $result[$key]['subjects'] = json_decode($result[$key]['subjects']);
    $result[$key]['traineeLicences'] = json_decode($result[$key]['traineeLicences']);
    $result[$key]['subjectsComments'] = json_decode($result[$key]['subjectsComments']);
    $result[$key]['failedSubjects'] = json_decode($result[$key]['failedSubjects']);
    $result[$key]['evaluations'] = json_decode($result[$key]['evaluations']);
    $result[$key]['history'] = json_decode($result[$key]['history']);
  }
} else {
  $result = [];
}

output($result);